export const statusFilter = (status = 1) => {
  if (status === -1) {
    return '已删除'
  }
  return status === 0 ? '已隐藏' : '已显示'
}

function fixedZero (val) {
  return val * 1 < 10 ? `0${val}` : val
}

const beforeFn = ({ d, h, m, s }, md) => {
  let temp
  if (d > 3) temp = md
  else if (d > 0) temp = `${d}天前`
  else if (h > 0) temp = `${h}小时前`
  else if (m > 0) temp = `${m}分前`
  else temp = `${s}秒前`
  return temp
}

export const timeFormat = (time = 0, type = 'format') => {
  const ymd = new Date(time * 1000)
  const md = `${new Date(time * 1000).getMonth() + 1}月${new Date(time * 1000).getDate()}日`

  if (type === 'before') time = (new Date().getTime() - (time * 1000)) / 1000

  const d = parseInt(time / 8.64e4, 10)
  const h = parseInt(time / 3600 % 24, 10)
  const m = parseInt(time / 60 % 60, 10)
  const s = parseInt(time % 60, 10)

  let result
  switch (type) {
    case 'countdown':
      result = `${d}天${h}小时${m}分${s}秒`
      break
    case 'before':
      result = beforeFn({ d, h, m, s }, md)
      break
    default:
      result = `${ymd.getFullYear()}年${fixedZero(ymd.getMonth() + 1)}月${fixedZero(ymd.getDate())}日
      ${fixedZero(ymd.getHours())}:${fixedZero(ymd.getMinutes())}`
  }
  return result
}

export const timeFilter = (timestamp) => {
  const date = new Date(timestamp * 1000)
  return date.toLocaleString('zh-CN', { hour12: false })
}

const statusMap = {
  200: 'Ok',
  201: 'Created',
  204: 'No Content',
  400: 'Bad Request',
  401: 'Unauthenticated',
  404: 'Not Found',
  405: 'Method Not Allowed',
  422: 'Unprocessable Entity',
  429: 'Too Many Request',
  500: 'Internal Server Error'
}

export const responseStatusFilter = (status) => {
  if (statusMap[status]) {
    return status + ' ' + statusMap[status]
  }

  return status
}
